package kr.co.creativeideas.gooq.activity;

import java.util.Timer;
import java.util.TimerTask;

import kr.co.creativeideas.gooq.common.StopWatch;
import kr.co.creativeideas.gooq.common.Tools;
import kr.co.creativeideas.gooq.view.LineCircleView;
import kr.co.creativeideas.gooq.view.TouchShapeView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {

	private static final int UPDATE_TIME = 1;
	private static final int UPDATE_STATE = 2;

	private TextView mTime;
	private ImageButton mStartButton;
	private StopWatch mStopWatch;
	private TextView mStart;
	private LineCircleView mLineCircle;	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainactivity);

		mStopWatch = new StopWatch();
		mTime = (TextView) findViewById(R.id.main_score);
		mTime.setTypeface(Tools.getTypeface(this, Tools.FONT_DIN));
		
		mStart = (TextView) findViewById(R.id.main_start);
		mStart.setTypeface(Tools.getTypeface(this, Tools.FONT_DIN_BLACKALTERNATE));
		
		mLineCircle = (LineCircleView)this.findViewById(R.id.linecircleview);
		
		mStartButton = (ImageButton) findViewById(R.id.main_btn);
		mStartButton.setBackgroundColor(0x00000000);
		mStartButton.setScaleType(ScaleType.CENTER);
		mStartButton.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				int action = event.getAction();
				
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					mStopWatch.running();
					mStartButton.setImageResource(R.drawable.button_on);
					mStart.setText("On Game");
					dispatchTouchEvent(event);
					break;
					
				case MotionEvent.ACTION_MASK:
					mStopWatch.running();
					break;
				
				case MotionEvent.ACTION_MOVE:
					mStopWatch.running();
					break;
					
				case MotionEvent.ACTION_UP:
					mStartButton.setImageResource(R.drawable.button_off);
					mStart.setText("Game Over");
					mStopWatch.stopped();			
					//dispatchTouchEvent(event);
					goRank();
					break;
				}				
				return false;
			}
		});
		
		Timer displayTimer = new Timer();
		displayTimer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				// mHandler.sendEmptyMessage(mHandler.obtainMessage(UPDATE_STATE,mStopWatch.getCurrentState()));
				mHandler.sendMessage(mHandler.obtainMessage(UPDATE_STATE,mStopWatch.getDisplay()));
			}
		}, 1, 1);

	}
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UPDATE_STATE:
				mTime.setText((String) msg.obj);
				break;
			case UPDATE_TIME:
				mTime.setText((String) msg.obj);
				break;
			default:
				super.handleMessage(msg);
			}
		}
	};

	private void goRank() {
		Intent intent = new Intent(this, ResultActivity.class);
		intent.putExtra("score", mTime.getText());
		//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
		startActivity(intent);		
		
	}
	
	@Override
	public void onPause() {
		super.onPause();		
    }
	
	@Override
	public void onResume() {
		super.onResume();
		mStart.setText("Push");
		mStopWatch.reset();
	}
}